package com.eims.sls_app.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.eims.ball.info.adapter.MyFragmentPagerAdapter;
import com.eims.hongbao.fragment.HongbaoOneFragment;
import com.eims.hongbao.fragment.HongbaoTwoFragment;
import com.eims.sls_app.ui.CommentActivity.MyHandler;
import com.eims.sls_app.utils.App;

import android.annotation.SuppressLint;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;
import bls.juejin.cn.R;
import progressBar.ProgressDialogBar;

public class MainFaHongbaoActivity extends MyFragmentActivity implements OnClickListener {
	private String TAG = "MainFaHongbaoActivity";
	private ViewPager mPager;
	private ImageView ivBottomLine;
	private TextView tvTabActivity, tvTabGroups;
	private ArrayList<Fragment> fragmentsList;
	private int currIndex = 0;
	private int bottomLineWidth;
	private int offset = 0;
	private int position_one;
	private int position_two;
	private int position_three;
	private Resources resources;
	private MyFragmentPagerAdapter adaper;

	private ImageButton ibBack;
	private String schemeid;
	private MyHandler myHandler;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main_fanhongbao_layout);
		myHandler = new MyHandler();
		resources = getResources();
		App.activityS.add(this);
		App.activityS1.add(this);
		InitWidth();
		
		InitTextView();
		InitViewPager();
	}

	private void InitTextView() {
		Log.e("11", "3");
		tvTabActivity = (TextView) findViewById(R.id.tv_tab_activity);
		tvTabGroups = (TextView) findViewById(R.id.tv_tab_groups);
		ibBack = (ImageButton) findViewById(R.id.btn_back_con);
		ibBack.setOnClickListener(this);
		tvTabActivity.setOnClickListener(new MyOnClickListener(0));
		tvTabGroups.setOnClickListener(new MyOnClickListener(1));

	}

	private void InitViewPager() {

		Log.e("11", "4");
		mPager = (ViewPager) findViewById(R.id.vPager);
		fragmentsList = new ArrayList<Fragment>();
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("userid", "С��");
		paramMap.put("age", 23);
		if (fragmentsList != null) {
			fragmentsList.clear();
			Log.e("11", "14");
		}
		// Fragment activityfragment = TestFragment.newInstance("Hello",paramMap);
		// Fragment groupFragment = TestFragment.newInstance("Hello Group.",paramMap2);

		//
		// fragmentsList.add(activityfragment);
		// fragmentsList.add(groupFragment);

		Fragment activityfragment =  HongbaoOneFragment.newInstance(schemeid);
		Fragment groupFragment =  HongbaoTwoFragment.newInstance(schemeid);
		fragmentsList.add(groupFragment);
		fragmentsList.add(activityfragment);

		adaper = new MyFragmentPagerAdapter(getSupportFragmentManager(), fragmentsList);
		adaper.notifyDataSetChanged();
		mPager.setAdapter(adaper);

		mPager.setCurrentItem(0);
		mPager.setOnPageChangeListener(new MyOnPageChangeListener());
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		fragmentsList.clear();
		Log.e("1", "5");
		super.onDestroy();
	}

	@Override
	public void onStop() {
		// TODO Auto-generated method stub
		Log.e("1", "6");
		super.onStop();
	}

	@Override
	public void onPause() {
		Log.e("1", "7");
		// TODO Auto-generated method stub
		super.onPause();
	}

	private void InitWidth() {

		Bundle bundle = getIntent().getExtras();
		schemeid = bundle.getString("schemeid", "");
		Log.e("11", schemeid);

		ivBottomLine = (ImageView) findViewById(R.id.iv_bottom_line);
		int h = MainFaHongbaoActivity.this.getResources().getDisplayMetrics().heightPixels;
		int w = MainFaHongbaoActivity.this.getResources().getDisplayMetrics().widthPixels;
		LayoutParams lp;
		lp = (LayoutParams) ivBottomLine.getLayoutParams();
		lp.width = w / 2;
		ivBottomLine.setLayoutParams(lp);

		bottomLineWidth = ivBottomLine.getLayoutParams().width;
		Log.d(TAG, "cursor imageview width=" + bottomLineWidth);
		DisplayMetrics dm = new DisplayMetrics();
		this.getWindowManager().getDefaultDisplay().getMetrics(dm);
		int screenW = dm.widthPixels;
		offset = (int) ((screenW / 2.0 - bottomLineWidth) / 2);

		position_one = (int) (screenW / 2.0);
		position_two = position_one * 2;

	}

	public class MyOnClickListener implements View.OnClickListener {
		private int index = 0;

		public MyOnClickListener(int i) {
			index = i;
		}

		@Override
		public void onClick(View v) {
			mPager.setCurrentItem(index);
		}
	};

	public class MyOnPageChangeListener implements OnPageChangeListener {

		@Override
		public void onPageSelected(int arg0) {
			Animation animation = null;
			switch (arg0) {
			case 0:
				if (currIndex == 1) {
					animation = new TranslateAnimation(position_one, 0, 0, 0);
					tvTabGroups.setTextColor(resources.getColor(R.color.black));
				}
				tvTabActivity.setTextColor(resources.getColor(R.color.main_fanhongabo_bg));
				break;
			case 1:
				if (currIndex == 0) {
					animation = new TranslateAnimation(0, position_one, 0, 0);
					tvTabActivity.setTextColor(resources.getColor(R.color.black));
				}
				tvTabGroups.setTextColor(resources.getColor(R.color.main_fanhongabo_bg));
				break;

			}
			currIndex = arg0;
			animation.setFillAfter(true);
			animation.setDuration(300);
			ivBottomLine.startAnimation(animation);
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}

		@Override
		public void onPageScrollStateChanged(int arg0) {
		}
	}

	@Override 
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_back_con:
			ProgressDialogBar.startProgress(MainFaHongbaoActivity.this, "sff", false);
			myHandler.sendEmptyMessageDelayed(4, 100);

			break;

		default:
			break;
		}
	}

	@SuppressLint("HandlerLeak")
	class MyHandler extends Handler {
		@Override
		public void handleMessage(Message msg) {

			switch (msg.what) {

			case 4:
				ProgressDialogBar.stopProgress();
				MainFaHongbaoActivity.this.finish();
				break;
			default:
				break;
			}
			super.handleMessage(msg);
		}
	}

}
